package com.tenement.controller;

import com.mybatisflex.core.paginate.Page;
import com.tenement.common.Axios;
import com.tenement.enums.RespBusiness;
import com.tenement.vo.QueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.tenement.entity.Notice;
import com.tenement.service.NoticeService;
import org.springframework.web.bind.annotation.RestController;
import java.io.Serializable;
import java.util.List;

/**
 *  控制层。
 *
 * @author BertXie
 * @since 1.0.1
 */
@RestController
@RequestMapping("/notice")
@RequiredArgsConstructor
@Api(tags = "公告管理接口")
public class NoticeController {

    private final NoticeService noticeService;

    @ApiOperation(value = "操作公告数据")
    @PutMapping("operator")
    public Axios operator(@RequestBody Notice notice)
    {
        return noticeService.getMapper().insertOrUpdateSelective(notice) > 0 ? Axios.success(RespBusiness.SUCCESS) : Axios.error(RespBusiness.FAIL);
    }

    @ApiOperation(value = "获取公告列表")
    @PreAuthorize("@exp.hasAuthority('tenement:notice:list')")
    @PostMapping("list")
    public Axios list(@RequestBody QueryVo queryVo)
    {
        Page<Notice> page = noticeService.list(queryVo);
        return Axios.success(page);
    }

    @ApiOperation(value = "删除公告数据")
    @DeleteMapping("remove")
    public Axios remove(@RequestBody List<Serializable> id)
    {
        return noticeService.getMapper().deleteBatchByIds(id) > 0 ? Axios.success(RespBusiness.SUCCESS) : Axios.error(RespBusiness.FAIL);
    }

    @ApiOperation(value = "改变当前文章的状态")
    @GetMapping("changeStatus/{id}/{status}")
    public Axios changeStatus(@PathVariable Serializable id,@PathVariable Short status)
    {
        return noticeService.changeStatus(id,status) ? Axios.success(RespBusiness.SUCCESS) : Axios.error(RespBusiness.FAIL);
    }

}
